#!/usr/bin/python
# Created by Jiawei
# Date Created: 2008/06/23

import cgi
import os
#import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from modules.models import CoverArticle
from external.libs.textile import Textiler


# the default page
class MainPage(webapp.RequestHandler):
    def get(self):
        article_Id = None
        article_content = None
        user = users.get_current_user()
        loginUrl = users.create_login_url(self.request.uri)
        logoutUrl = users.create_logout_url(self.request.uri)
        isAdminUser = users.is_current_user_admin()
        
        articles = CoverArticle.all().filter("is_display", True).order("-create_date")
        articleList  = []

        for article in articles:
            articleList.append({'id':article.key().id(),'content':Textiler(article.content).process()})
            
        template_values = {
            'isAdminUser': isAdminUser,
            'loginUrl': loginUrl,
            'logoutUrl': logoutUrl,
            'user': user,
            'articleList': articleList
            }
        path = os.path.join(os.path.dirname(__file__), 'templates/home.html')
        self.response.out.write(template.render(path, template_values))
        
class SubmitPost(webapp.RequestHandler):
    def post(self):
        article = None
        content = self.request.get('content').decode('utf-8')
        article_Id = self.request.get('article_Id')
        if (article_Id):
            article = CoverArticle.get_by_id(int(article_Id))
        else:
            article = CoverArticle()
            cur_display_article = CoverArticle.all().filter("is_display", True).get()
            if (cur_display_article):
                cur_display_article.is_display = False
                cur_display_article.put()
            
        article.content = content
        article.put()
        self.response.out.write(article.key().id())
        
class ArticleHandler(webapp.RequestHandler):
    def get(self):
        article_id = None
        action = self.request.get('action')
        if (action == 'edit'):
            page_title = 'Edit This Page'
            button_text = 'Update this page'
            article_id = self.request.get('article_id')
            article = CoverArticle.get_by_id(int(article_id))
            article_content = article.content
            article_title = article.title
            
            template_values = {
                'article_id': article_id,
                'article_content': article_content,
                'page_title':page_title,
                'button_text':button_text,
                'action':'update',
                'article_title': article_title
                }
            path = os.path.join(os.path.dirname(__file__), 'templates/home_edit.html')
            self.response.out.write(template.render(path, template_values))
        elif (action == 'delete'):
            article_id = self.request.get('article_id')
            article = CoverArticle.get_by_id(int(article_id))
            article.delete()
            self.redirect('/', True)
        elif (action == 'new'):
            page_title = 'Add New Page'
            button_text = 'Add this page'
            
            template_values = {
                'page_title':page_title,
                'button_text':button_text,
                'action':'new'
                }
            path = os.path.join(os.path.dirname(__file__), 'templates/home_edit.html')
            self.response.out.write(template.render(path, template_values))
            
    def post(self):
        action = self.request.get('action')
        if (action == 'new'):
            article = CoverArticle()
            article.content = self.request.get('article_content')
            article.put()
            self.redirect('/', True)
        elif (action == 'update'):
            article_id = self.request.get('article_id')
            article = CoverArticle.get_by_id(int(article_id))
            article.title = self.request.get('article_title')
            article.content = self.request.get('article_content')
            article.put()
            self.redirect('/', True)